<template>
  <div class="site-search-bar">
    <div class="site-search-bar-content">
      <div class="logo">
        <div class="login-img">
          <img src="@/assets/images/site_logo.jpg" />
        </div>
      </div>
      <div class="supplier" v-if="route.meta.ori === 'goods' || route.meta.ori === 'store' || route.meta.ori === 'qualica'">
        <el-tooltip class="box-item" effect="dark" :content="_supplierStore.supplierName" placement="bottom-start">
          <div>
            <span class="supplier-name">{{ _supplierStore.supplierName }}</span>
          </div>
        </el-tooltip>
        <el-dropdown trigger="hover" @command="menuTrigger">
          <div style="border: 0">
            <img v-if="!route.query.keyword" class="store-icon" src="@/assets/images/dianpuxiao.png" />
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="ToStore">进入店铺</el-dropdown-item>
              <el-dropdown-item command="showQualica">查看资质</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div class="search">
        <div class="search-bar">
          <el-input class="search-input" v-model="searchData.keyword" @keyup.enter.prevent="onSearchClick()">
            <template #prefix>
              <img src="@/assets/images/sousuo.png" class="icon" />
            </template>
          </el-input>

          <Button label="搜索" class="search-button" @click="onSearchClick()" />
          <Button class="search-button1" label="搜本店" v-if="route.path === '/store'" @click="onSearchClick()" />
        </div>
      </div>

      <div v-if="route.path !== '/store'" @click="router.push('/shoppingCart')" class="shoppingCart">
        <img src="@/assets/images/gouwuche.png" />
        <span style="display: inline-block">采购单</span>
        <span class="badge">{{ count }}</span>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, ref, computed, onMounted, watch, onUpdated } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import api from '@/api'
import { cartStore } from '@/store/cart'
import { SupplierStore } from '@/store/supplier'
import Button from '@/components/button/button.vue'

const emit = defineEmits(['search'])

const router = useRouter()
const route = useRoute()
const ori = route.meta.ori

const _cartStore = cartStore()
const _supplierStore = SupplierStore()

const searchData = reactive({
  keyword: route.query.keyword || '',
  goodsId: []
})

const count = computed(() => {
  return _cartStore.list.length
})

const onSearchClick = () => {
  if (!searchData.keyword) {
    return
  }
  if (route.path === '/store' || route.path === '/search') {
    // 本店搜索
    let { href } = router.resolve({
      path: route.path,
      query: {
        ...route.query,
        keyword: searchData.keyword
      }
    })
    window.open(href, '_self')
    window.location.reload()
  } else {
    // 全局搜索
    if (route.name !== 'Search') {
      let { href } = router.resolve({
        path: '/search',
        query: {
          keyword: searchData.keyword
        }
      })
      window.open(href, '_blank')
    } else {
      router.replace({ path: '/search', query: { keyword: searchData.keyword } })
    }
  }
}

const onSearchSelfClick = () => {
  let { href } = router.resolve({
    path: '/store',
    query: {
      ...route.query,
      keyword: searchData.keyword
    }
  })
  window.open(href, '_self')
}

const loadData = async () => {
  let { data, err } = await api.get('/medinstrument/site/cart')
  if (err) {
    ElMessage.error(err)
  } else {
    let goodsIds = data.list.flatMap((e) => {
      return e.cartItemList.map((item) => {
        return item.goodsId
      })
    })
    _cartStore.set(goodsIds)
  }
}

const menuTrigger = (cmd) => {
  if (cmd === 'ToStore') {
    if (route.query.supplierId) {
      let { href } = router.resolve({
        path: '/store',
        query: { supplierId: route.query.supplierId }
      })
      window.open(href, '_blank')
    } else {
      let { href } = router.resolve({
        path: '/store',
        query: { supplierId: _supplierStore.supplierId }
      })
      window.open(href, '_blank')
    }
  } else if (cmd === 'showQualica') {
    if (route.query.supplierId) {
      let { href } = router.resolve({
        path: '/supplierQucalition',
        query: { supplierId: route.query.supplierId }
      })
      window.open(href, '_blank')
    } else {
      let { href } = router.resolve({
        path: '/supplierQucalition',
        query: { supplierId: _supplierStore.supplierId }
      })
      window.open(href, '_blank')
    }
  }
}

onMounted(() => {
  loadData()
})
</script>
<style scoped lang="scss">
.site-search-bar {
  box-sizing: border-box;
  background-color: #fff;
  padding-top: 10px;
  padding-bottom: 51px;
  border-bottom: 2px solid #16bfa7;
  box-sizing: border-box;

  .site-search-bar-content {
    width: 1080px;
    height: 90px;
    margin: 0 auto;
    display: flex;
    align-items: center;

    .logo {
      display: flex;
      align-items: center;

      .login-img {
        padding-right: 15px;

        img {
          width: 100px;
          height: 50px;
        }
      }
    }

    .title {
      font-size: 20px;
      line-height: 28px;
    }

    .supplier {
      display: flex;
      align-items: center;
      line-height: 28px;
      font-size: 20px;
      color: var(--color-text);
      font-weight: 600;
      padding-left: 15px;
      margin-right: 40px;
      border-left: 1px solid #dcdfe6;

      &:hover {
        color: var(--color-primary);
      }

      .supplier-name {
        display: inline-block;
        max-width: 240px;
        height: 28px;
        overflow: hidden;
      }

      .store-icon {
        width: 20px;
        height: 20px;
        line-height: 28px;
        margin-left: 10px;
      }
    }

    .search {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin: 0 40px 0 auto;

      .search-bar {
        display: flex;
        width: 100%;

        .search-input {
          position: relative;
          display: flex;

          :deep(.el-input__wrapper) {
            width: 323px;
          }

          .icon {
            position: absolute;
            left: 10px;
          }
        }

        .search-button {
          width: 90px;
          height: 38px;
          line-height: 22px;
          font-size: 16px;
          border-radius: 0 100px 100px 0;
          border: 1px solid #3e424c;
          color: #fff;
          background-color: #3e424c;
        }

        .search-button1 {
          width: 90px;
          height: 38px;
          border: 1px solid #3e424c;
          border-radius: 100px;
        }

        .search-button1:hover {
          color: #606266;
          background-color: #fff;
        }
      }
    }

    .shoppingCart {
      display: flex;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
      width: 134px;
      height: 36px;
      border: 1px solid rgba(62, 66, 76, 1);
      border-radius: 19px;
      cursor: pointer;
      img {
        width: 18px;
        height: 18px;
        margin-right: 7px;
      }
      .badge {
        width: 20px;
        height: 14px;
        line-height: 14px;
        border-radius: 19px;
        background-color: #d33a20;
        font-size: 12px;
        color: #fff;
        padding: 2px 4px;
        margin-left: 14px;
        text-align: center;
      }
    }
  }
}

.search-bar:deep() .el-input__inner {
  border: 1px solid rgba(62, 66, 76, 1);
  padding: 0 10px 0 40px;
  flex: 1;
  height: 38px;
  border-radius: 50px 0 0 50px;
}

.search-bar:deep() .el-input__wrapper {
  padding: 0;
  box-shadow: none;
  width: 100%;
}

.el-tooltip__trigger {
  cursor: pointer;
}
</style>
